
CXX		= g++
CC		= gcc
BASE_DIR = .
#BASE_INC_DIR = ./
#所包含的头文件所在的位置
INC_DIR = ./include /usr/include/mysql/
#源文件所放的位置
SRC_DIR = ./src
#如果源文件在多个不同的目录下就写成下方空格隔开的形式
#SRC_DIR = dir1 dir2 dir3 ... 

#生成的.o文件放置的目录
OBJ_DIR = ./objs
#输出的可执行文件所放的目录
OUT_DIR = ./output

PREDEFINE_FLAG = -DDEBUG

TARGET1 = $(OUT_DIR)/logic_server
TARGET1_FILE = ./server/logic_server.cpp

TARGET2 = $(OUT_DIR)/gateway_server
TARGET2_FILE = ./server/gateway_server.cpp

INC_FLAG = $(foreach DIR,$(INC_DIR),$(addprefix -I,$(DIR)))
LIB_DIR_FLAG	= -L$(BASE_DIR)/objs -L/usr/lib64/mysql/
LIB_FLAG 		= -lmysqlclient

LD_FLAG		= $(LIB_DIR_FLAG) $(LIB_FLAG)
CFLAGS		= -g -pipe -Werror -fPIC
CPPFLAGS	= -g -pipe -Werror -fPIC

SRC		= $(foreach DIR,$(SRC_DIR),$(wildcard $(DIR)/*.cpp $(DIR)/*.c))

OBJECTS	= $(addprefix $(OBJ_DIR)/,$(patsubst ./src/%,%,$(patsubst %.c, %.o,$(patsubst %.cpp,%.o,$(SRC)))))

#make的话优先执行all，所以想要都执行的都写在all中即可
#因为logic_server和gateway_server都包含了main函数，所以不能同时执行运行，所以我分开写成TARGET1和TARGET2
all: Make_Preparation

Make_Preparation:
	$(foreach dir, $(SRC_DIR), $(shell mkdir -p $(OBJ_DIR)/$(dir)))
	$(shell mkdir -p $(OUT_DIR))

$(TARGET1): $(OBJECTS) $(TARGET1).o
	$(CXX) -g -o $@ $(OBJECTS) $(TARGET1).o $(LD_FLAG)
$(TARGET1).o: $(TARGET1_FILE)
	$(CXX) $(CPPFLAGS) $(INC_FLAG) $(PREDFINE_FLAG) -c $< -o $@

$(TARGET2): $(OBJECTS) $(TARGET2).o
	$(CXX) -g -o $@ $(OBJECTS) $(TARGET2).o $(LD_FLAG)
$(TARGET2).o: $(TARGET2_FILE)
	$(CXX) $(CPPFLAGS) $(INC_FLAG) $(PREDFINE_FLAG) -c $< -o $@

debug:
	@echo "INC_FLAG = " $(INC_FLAG)
	@echo "SRC		= " $(SRC)
	@echo "SRC_DIR	= " $(SRC_DIR)
	@echo "OBJ_DIR	= " $(OBJ_DIR)
	@echo "OBJECTS 	= " $(OBJECTS)
	@echo "TARGET	= " $(TARGET)

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
	$(CXX) $(CPPFLAGS) $(INC_FLAG) $(PREDEFINE_FLAG) -c $< -o $@

$(LIB_DIR)/%.o: %.c
	$(CC) $(CFLAGS) $(INC_FLAG) $(PREDEFINE_FLAG) -c $< -o $@

.PHONEY: clean
clean:
	rm -rf $(TARGET) $(OBJECTS)
